<?php

////////////////////////////////////////////////////////////////////////////////
// Get Featured Post Image
////////////////////////////////////////////////////////////////////////////////
function get_featured_slider_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
  	$img_dir = get_bloginfo('template_directory');
    $first_img = $img_dir . '/images/feat-default.jpg';
  }
  return $first_img;
}

////////////////////////////////////////////////////////////////////////////////
// Get Post Image
////////////////////////////////////////////////////////////////////////////////
function get_post_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
  	$img_dir = get_bloginfo('template_directory');
    $first_img = $img_dir . '/images/post-default.jpg';
  }
  return $first_img;
}

// to prevent  WP < 2.9 breaking
if (function_exists('add_theme_support')) {
add_theme_support( 'post-thumbnails' );
}


////////////////////////////////////////////////////////////////////////////////

// Standard Post Excerpt

////////////////////////////////////////////////////////////////////////////////

function the_post_excerpt($excerpt_length=50, $allowedtags='', $filter_type='none', $use_more_link=false, $more_link_text="Read More", $force_more_link=false, $fakeit=1, $fix_tags=true) {

	if (preg_match('%^content($|_rss)|^excerpt($|_rss)%', $filter_type)) {

		$filter_type = 'the_' . $filter_type;

	}

	$text = apply_filters($filter_type, get_the_post_excerpt($excerpt_length, $allowedtags, $use_more_link, $more_link_text, $force_more_link, $fakeit));

	$text = ($fix_tags) ? balanceTags($text) : $text;

	echo $text;

}

function get_the_post_excerpt($excerpt_length, $allowedtags, $use_more_link, $more_link_text, $force_more_link, $fakeit) {

	global $id, $post;

	$output = '';

	$output = $post->post_excerpt;

	if (!empty($post->post_password)) { // if there's a password

		if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie

			$output = __('There is no excerpt because this is a protected post.');

			return $output;

		}

	}

	// If we haven't got an excerpt, make one.

	if ((($output == '') && ($fakeit == 1)) || ($fakeit == 2)) {

		$output = $post->post_content;

		$output = strip_tags($output, $allowedtags);

        $output = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $output );

		$blah = explode(' ', $output);

		if (count($blah) > $excerpt_length) {

			$k = $excerpt_length;

			$use_dotdotdot = 1;

		} else {

			$k = count($blah);

			$use_dotdotdot = 0;

		}

		$excerpt = '';

		for ($i=0; $i<$k; $i++) {

			$excerpt .= $blah[$i] . ' ';

		}


		if (($use_more_link && $use_dotdotdot) || $force_more_link) {

			$excerpt .= "...&nbsp;<a href=\"". get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>";

		} else {

			$excerpt .= ($use_dotdotdot) ? '...' : '';

		}

		 $output = $excerpt;

	} // end if no excerpt

	return $output;

}



////////////////////////////////////////////////////////////////////////////////
// Sidebar Widget
////////////////////////////////////////////////////////////////////////////////

if ( function_exists('register_sidebar') )
	register_sidebar(array('name'=>'sidebar',
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div></div>',
        'before_title' => '<h2>',
        'after_title' => '</h2><div id="maga"></div><div class="widget2">',
    ));


////////////////////////////////////////////////////////////////////////////////
// Theme Option
////////////////////////////////////////////////////////////////////////////////

$themename = "Fytuts";
$shortname = str_replace(' ', '_', strtolower($themename));

function get_theme_option($option)
{
	global $shortname;
	return stripslashes(get_option($shortname . '_' . $option));
}

function get_theme_settings($option)
{
	return stripslashes(get_option($option));
}
$wp_dropdown_rd_admin = $wpdb->get_results("SELECT $wpdb->term_taxonomy.term_id,name,description,count FROM $wpdb->term_taxonomy LEFT JOIN $wpdb->terms ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id WHERE parent > -1 AND taxonomy = 'category' AND count != '0' GROUP BY $wpdb->terms.name ORDER by $wpdb->terms.name ASC");
$wp_getcat = array();
foreach ($wp_dropdown_rd_admin as $category_list) {
$wp_getcat[$category_list->term_id] = $category_list->name;
}
$category_bulk_list = array_unshift($wp_getcat, "Choose a category:");
$number_entries = array("Number of post:","1","2","3","4","5","6","7","8","9","10");
$crop_position = array("Choose Crop Position","middle","middleleft","middleright","topcenter","topleft","topright","bottomcenter","bottomleft","bottomright");

$options = array (


    array(	"name" => "Blog Header Settings",
            "type" => "heading",
            ),

			array(	"name" => "Use Custom Logo On The Blog Header?<br /><em>*Disable by default, Choose Yes to enable it.</em>",
			"id" => $shortname."_header_logo_activate",
            "type" => "select",
            "std" => "No",
			"options" => array("No", "Yes")),

			array(	"name" => "Insert The Full URL Location Of Your Logo Here <br /><em>*leave blank if not use</em>",
			"id" => $shortname."_logo_url",
            "type" => "text",
            "box" => "social",
            "std" => "",
            ),
	
			
   		array(	"name" => "Insert Header Banner HTML Code (Header)
			<br /><em>*Recommended Size 468 x 60</em>
			<br /><em>*leave blank if not use</em>",
			"id" => $shortname."_header_banner",
            "type" => "textarea",
            "std" => "",
            ),

			array(	"name" => "</div></div>",
            "type" => "close",
            ),
		

    array(	"name" => "Google Analytics Settings",
            "type" => "heading",
            ),

   			array(	"name" => "Enable Google Adsense On Single Page<br /><em>*default are disable, you can activate it by choosing enable</em>",
			"id" => $shortname."_adsense_single_activate",
            "type" => "select",
            "std" => "Disable",
			"options" => array("Disable", "Enable")),

			array(	"name" => "Insert Google Adsense Code For Single Page Here<br />
			<em>*Copy &amp; Paste Your Google Code Or Affiliate Network Banner Code Here</em>",
			"id" => $shortname."_adsense_single",
            "type" => "textarea",
            "std" => "",
            ),
			

    		array(	"name" => "Insert Google Analytics code <br /><em>*optional - leave it blank if not using</em>",
			"id" => $shortname."_google_analytics",
            "type" => "textarea",
            "std" => "",
            ),

     		array(	"name" => "</div></div>",
            "type" => "close",
            ),

 array(	"name" => "Blog Footer Settings",
            "type" => "heading",
            ),

			array(	"name" => "Use Custom Logo On The Blog Footer?<br /><em>*Disable by default, Choose Yes to enable it.</em>",
			"id" => $shortname."_footer_logo_activate",
            "type" => "select",
            "std" => "No",
			"options" => array("No", "Yes")),

			array(	"name" => "Insert The Full URL Location Of Your Logo Here <br /><em>*leave blank if not use</em>",
			"id" => $shortname."_footer_logo_url",
            "type" => "text",
            "std" => "",
            ),	
				array(	"name" => "</div></div>",
            "type" => "close",
            ),

	array(	"name" => "125 x 125 Banners Advertisement Settings",
            "type" => "heading",
            ),
			
			array(	"name" => "Enable <strong>125 x 125 Banners</strong> On Sidebar?<br /><em>*Disable by default, Choose Yes to enable it.</em>",
			"id" => $shortname."_sponsor_activate",
            "type" => "select",
            "std" => "No",
			"options" => array("No", "Yes")),
				

			array(	"name" => "Insert Sponsor Banner One HTML Code<br /><em>*leave blank if not use</em>",
			"id" => $shortname."_sponsor_banner_one",
            "type" => "textarea",
            "std" => "",
            ), 

			array(	"name" => "Insert Sponsor Banner Two HTML Code<br /><em>*leave blank if not use</em>",
			"id" => $shortname."_sponsor_banner_two",
            "type" => "textarea",
            "std" => "",
            ),

			array(	"name" => "Insert Sponsor Banner Three HTML Code<br /><em>*leave blank if not use</em>",
			"id" => $shortname."_sponsor_banner_three",
            "type" => "textarea",
            "std" => "",
            ),

			array(	"name" => "Insert Sponsor Banner Four HTML Code<br /><em>*leave blank if not use</em>",
			"id" => $shortname."_sponsor_banner_four",
            "type" => "textarea",
            "std" => "",
            ),

			array(	"name" => "Insert Sponsor Banner Five HTML Code<br /><em>*leave blank if not use</em>",
			"id" => $shortname."_sponsor_banner_five",
            "type" => "textarea",
            "std" => "",
            ),

			array(	"name" => "Insert Sponsor Banner Six HTML Code<br /><em>*leave blank if not use</em>",
			"id" => $shortname."_sponsor_banner_six",
            "type" => "textarea",
            "std" => "",
            ),

			array(	"name" => "</div></div>",
            "type" => "close",
            ),



);

function mytheme_admin_panel(){ if ((function_exists("check_theme_footer") || function_exists("check_theme_header"))) {

echo "<div id=\"admin-options\"> ";

global $themename, $shortname, $options;
if ( $_REQUEST['saved'] ) echo '<div id="update-option" class="updated fade"><strong>'.$themename.' settings saved.</strong></div>';
if ( $_REQUEST['reset'] ) echo '<div id="update-option" class="updated fade"><strong>'.$themename.' settings reset.</strong></div>';
?>

<h4><?php echo "$themename"; ?> Theme Options</h4>


<form action="" method="post">

<?php foreach ($options as $value) { ?>

<?php switch ( $value['type'] ) { case 'heading': ?>

<div class="get-option">

<h2><?php echo $value['name']; ?></h2>

<div class="option-save">

<?php
break;
case 'text':
?>

<div class="description"><?php echo $value['name']; ?></div>
<p><input name="<?php echo $value['id']; ?>" class="myfield" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if (

get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" /></p>

<?php
break;
case 'select':
?>

<div class="description"><?php echo $value['name']; ?></div>
<p><select name="<?php echo $value['id']; ?>" class="myselect" id="<?php echo $value['id']; ?>">
<?php foreach ($value['options'] as $option) { ?>
<option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
<?php } ?>
</select>
</p>

<?php
break;
case 'textarea':
$valuex = $value['id'];
$valuey = stripslashes($valuex);
$video_code = get_settings($valuey);
?>

<div class="description"><?php echo $value['name']; ?></div>
<p><textarea name="<?php echo $valuey; ?>" class="mytext" cols="40%" rows="8" /><?php if ( get_settings($valuey) != "") { echo stripslashes($video_code); }

else { echo $value['std']; } ?></textarea></p>

<?php
break;
case 'close':
?>

<div class="clearfix"></div>
</div><!-- OPTION SAVE END -->

<div class="clearfix"></div>
</div><!-- GET OPTION END -->

<?php
break;
default;
?>


<?php
break; } ?>

<?php } ?>

<p class="save-p">
<input name="save" type="submit" class="sbutton" value="Save Options" />
<input type="hidden" name="action" value="save" />
</p>
</form>

<form method="post">
<p class="save-p">
<input name="reset" type="submit" class="sbutton" value="Reset Options" />
<input type="hidden" name="action" value="reset" />
</p>
</form>

</div><!-- ADMIN OPTIONS END -->

<?php } else { echo ("Well, You Done It..You Just Modified Some Codes!"); } }

function mytheme_admin_register() {
global $themename, $shortname, $options;
if ( $_GET['page'] == basename(__FILE__) ) {
if ( 'save' == $_REQUEST['action'] ) {
foreach ($options as $value) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
foreach ($options as $value) {
if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
header("Location: themes.php?page=functions.php&saved=true");
die;
} else if( 'reset' == $_REQUEST['action'] ) {
foreach ($options as $value) {
delete_option( $value['id'] ); }
header("Location: themes.php?page=functions.php&reset=true");
die;
}
}
add_theme_page($themename." Options", "Theme Options", 'edit_themes', basename(__FILE__), 'mytheme_admin_panel');
}

function mytheme_admin_head() { ?>
<link href="<?php bloginfo('template_directory'); ?>/css/admin-panel.css" rel="stylesheet" type="text/css" />
<?php }

add_action('admin_head', 'mytheme_admin_head');
add_action('admin_menu', 'mytheme_admin_register');
if (!empty($_REQUEST["theme_credit"])) {

	theme_usage_message(); exit();

	}

	function theme_usage_message() {

	if (empty($_REQUEST["theme_credit"])) {

	$theme_credit_false = get_bloginfo("url") . "/index.php?theme_credit=false";

	echo "<meta http-equiv=\"refresh\" content=\"0;url=$theme_credit_false\">"; exit();

	} else {

    $rk_url = get_bloginfo('template_directory');
	$homepage = get_bloginfo('home');

	echo ("<div style=\"width:800px; margin:auto; padding:15px; text-align:center; background-color:#FFFFFF; border:5px solid #FF0000; color:#000000\">");
    echo ("<div><img src=\"$rk_url/images/error.jpg\" alt=\"Error\" /></div>");
    echo ("<div style=\"font-size:36px;\"><b>Opps..You Have Modified The Footer Links..</b></div>");
    echo ("<div style=\"font-size:15px;\"><b>This Theme Is Released Free For Use Under Creative Commons Licence. All Links In The Footer Must Remain Intact AS IS. These Links Are All Family Friendly And Will Not Hurt Your Site In Any Way. Please Appreciate These Supporters Effort In Providing You This Great Theme For Free.</b></div>");
    echo ("<div style=\"font-size:16px; padding-top:20px;\"><b>Please Follow These Steps To Restore The Footer: <ol><li>Please open the default folder, you'll find footer.php inside</li><li>Copy & paste it to overwrite the current footer.php you've modified.</li><li>Finally, refresh your page <a href=\"$homepage\">HERE</a> to go back to your homepage.</li></ol></b></div></div>");

	}

}

function check_theme_footer() {

	$l = 'Designed and Developed by <a href="http://www.renehornig.com/" title="webdesigner" target="_blank">webdesigner</a><br /><span id="credits">Thanks to <a href="http://temawp.com/" target="_blank">WordPress Themes</a> | <a href="http://www.fatakat.com/" target="_blank">Fatakat</a> and Gunstig ein <a href="http://www.notebook-online-kaufen.com/">Notebook-online-kaufen.com</a></span><br />';

	$f = dirname(__file__) . "/footer.php";

	$fd = fopen($f, "r");

	$c = fread($fd, filesize($f));

	fclose($fd); if (strpos($c, $l) == 0) {

	theme_usage_message();

    die;

	}

}

	check_theme_footer();


if(!function_exists('get_sidebar')) {

	function get_sidebar() {

	check_theme_header();

	get_sidebar();

	}
}

function check_theme_header() {

    if (!(function_exists("functions_file_exists") && function_exists("theme_footer_v")))
    {
    theme_usage_message();
    die;
    }
}

function functions_file_exists() {

	if (!file_exists(dirname(__file__) . "/functions.php") || !function_exists("theme_usage_message") )
	{
    theme_usage_message();
	die;
    }
}

add_action('wp_head', 'check_theme_header');
add_action('wp_head', 'functions_file_exists');?>